<?php
// File; edit-store-item.php
// Auth: Richard Foard
// Orig: 20Jan2014
//
require('../includes/class.main.php');
$manager = new MeetingManager();

include('../template/admin-header.php');
include('../includes/format-price.php');

	$lookup = new MeetingLookup();
	$db = new DB();
	
	$outcome = "";
	
	if (!isset($_POST['submit'])) {

		if (isset($_GET['addmode'])) { // add mode, show empty form
			$_SESSION['uiState'] = "ADD";
			
			$page = "";
			$newCategory = "";
			$itemNumber = "";
			$item = "";
			$price = "";
			$description = "";
			$discontinued = "NO";
		}
		
		else { // change or delete mode, populate form
			$rownr = $_GET['row'];
			$_SESSION['drow'] = $rownr;
			$_SESSION['uiState'] = "CHANGE_DEL";

			$item_row = $db->setQuery("select * from store where row = $rownr");
			$row = $item_row->fetch_assoc();
			
			$page			= $row['page'];
			$newCategory	= "";
			$itemNumber		= $row['itemNumber'];
			$item			= $row['item'];
			$price			= format_price($row['price']);
			$description	= $row['description'];
			$discontinued	= $row['discontinued'];
			if ($discontinued == "") $discontinued = "NO";
		}
	}
	
	else { // Form was posted for meeting deletion, change, or addition (or operation was cancelled)

		if (isset($_POST['page'])) {
			$page = $_POST['page'];
		}
		
		else {
			$page = "";
		}
		
		$newCategory	= $_POST['newCategory'];
		$itemNumber		= $_POST['itemNumber'];
		$item			= $_POST['item'];
		$price			= format_price($_POST['price']);
		$description	= $_POST['description'];
		$discontinued	= $_POST['discontinued'];
		
		if ($_POST['submit'] == "Delete Item") {

			// delete row and set message
			$rownr = $_SESSION['drow'];
			$query_result = $db->setQuery("delete from store where row = $rownr");

			$dberr = $db->getError();
			if ($dberr == "") {
				$outcome = "<a href=\"index.php\" style='color: blue;'>Item deleted, click 'Continue' to proceed.</a>";
			}

			else {
				$outcome = "<p style='color: red;'>Operation failed. [advice=$dberr]</p>";
			}
				
			$_SESSION['uiState'] = "CONTINUE_DELETE";
		}
		
		else { // adding or changing

			// Standardize and validate form information
			$errortext = "";
			
			$discontinued = strtoupper(trim($discontinued));
			
			if ($newCategory !== "") {
				$page = $newCategory;
			}
			
			else if ($page == "") {
				$errortext = "Please select a category or enter a new one.";
			}

			// Proceed if no errors found
			if ($errortext == "") {

				if ($_POST['submit'] == "Add Item") {

					// add row and set message
					$query_result = $db->setQuery("insert into store "
						. "(page, itemNumber, item, price, description, discontinued) "
						. "values (\"$page\",\"$itemNumber\",\"$item\",\"$price\",\"$description\",\"$discontinued\")");

					$dberr = $db->getError();
					if ($dberr != "") {
						$outcome ="<p style='color: red;'>Addition failed. [advice=$dberr]</p>";
					}
					
					else {
						$outcome = "<a href=\"index.php\" style='color: blue;'>Item added, click 'Continue' to proceed.</a>";
					}

					$_SESSION['uiState'] = "CONTINUE_ADD";
				}
				
				else if ($_POST['submit'] == 'Save Changes') {

					// update row and set message
					$rownr = $_SESSION['drow'];
					$query_result = $db->setQuery("update store SET "
						. "page=\"$page\","
						. "itemNumber=\"$itemNumber\","
						. "item=\"$item\","
						. "price=\"$price\","
						. "description=\"$description\","
						. "discontinued=\"$discontinued\""
						. " where row = $rownr");

					$dberr = $db->getError();
					if ($dberr != "") {
						$outcome = "<p style='color: red;'>Update failed. [advice=$dberr]</p>";
					}
					
					else {
						$outcome = "<a href=\"index.php\" style='color: blue;'>Item changed, click 'Continue' to proceed.</a>";
					}

					$_SESSION['uiState'] = "CONTINUE_CHANGE_DEL";
				}
			}
			
			else {
				$outcome = "<p><ul style='color: red;'>" . $errortext . "</ul></p>";
			}
		}
	}

	$abled = "";
	if (isset($_SESSION['uiState'])) {

		if ($_SESSION['uiState'] == "CONTINUE_ADD"
			|| $_SESSION['uiState'] == "CONTINUE_DELETE"
			|| $_SESSION['uiState'] == "CONTINUE_CHANGE_DEL") $abled = "disabled";
	}
?>

<div class="left">
<?php 
include('../admin/menu.php');
?>

</div>

<div class="right">
	<h1> <b><span class="blue">
	<?php if ($_SESSION['uiState'] == "ADD") { ?>
		Add Store Item
	<?php } else if($_SESSION['uiState'] == "CONTINUE_ADD") { ?>
		(Item Added)
	<?php } else if ($_SESSION['uiState'] == "CONTINUE_DELETE") { ?>
		(Item Deleted)
	<?php } else if ($_SESSION['uiState'] == "CONTINUE_CHANGE_DEL") { ?>
		(Item Changed)
	<?php } else { ?>
		Change or Delete Store Item
	<?php } ?>
	</span></b>
	</h1>

	<?php 
	if (isset($outcome) && $outcome != "") {
		echo $outcome; 
	}
	
	$lookup = new StoreLookup();
	$pages = $lookup->getPages();
	?>
	<br>
	
	<form action="edit-store-item.php" method="post">
		<div class="adminForm">
		
			<span class="label longCol1"><b>Select Category</b></span>
			<select name="page" size="6" style="width: 26em" autofocus <?php echo $abled; ?> >
			<?php
			while ($category = $pages->fetch_row()) {
			
				if ($category[0] == $page) {
					echo "<option value='$page' selected>$page</option>\n";
				}
				
				else {
					echo "<option>$category[0]</option>";
				}
			}
			?>
			</select>
			<br />

			<span class="label longCol1">-OR- Enter New Category</span>
			<input type="text" name="newCategory" maxlength="255" style="width:26em" <?php echo $abled; ?> value="<?php echo $newCategory; ?>" />
			<br />
			<br />

			<span class="label longCol1"><b>Item Number</b></span>
			<input type="text" class="required" name="itemNumber" required maxlength="255" style="width:7em" <?php echo $abled; ?> value="<?php echo $itemNumber; ?>" />
			<br />

			<span class="label longCol1"><b>Item</b></span>
			<input type="text" class="required" name="item" required maxlength="255" style="width:26em" <?php echo $abled; ?> value="<?php echo $item; ?>" />
			<br />

			<span class="label longCol1"><b>Price</b></span>
			<input type="text" class="required" name="price" required maxlength="255" style="width:4em" <?php echo $abled; ?> value="<?php echo $price; ?>" />
			<br />

			<span class="label longCol1">Description</span>
			<input type="text" name="description" maxlength="255" style="width:26em" <?php echo $abled; ?> value="<?php echo $description; ?>" />
			<br />

			<span class="label longCol1"><b>Discontinued</b></span>
			<select name="discontinued" class="required" <?php echo $abled; ?> >
				<?php if ($discontinued == "") $discontinued="NO" ?>
				<option <?php if ($discontinued == "NO") echo "value='$discontinued' selected"; ?>>NO</option>
				<option <?php if ($discontinued == "YES") echo "value='$discontinued' selected"; ?>>YES</option>
			</select>
			<br>


			<br />
			<br />

			<div class="submitBlock">
				<?php if ($_SESSION['uiState'] == "CHANGE_DEL") { ?>
				<input type="submit" value="Save Changes" name="submit">
				<input type="submit" value="Delete Item" name="submit">
				<a class="label" href="index.php" style='color: red;'>Cancel</a>
				<?php } else if ($_SESSION['uiState'] == "ADD") { ?>
				<input type="submit" value="Add Item" name="submit">
				<a class="label" href="index.php" style='color: red;'>Cancel</a>
				<?php } else { ?>
				<a class="label" href="index.php" style='color: blue;'>Continue</a>
				<?php } ?>
			</div>
		</div>
	</form>
</div>

<?php 
include('../template/admin-footer.php'); 
?>

</body>
</html>
